<?php

function form_input($array)
{
	$val = (isset($array['value']) ? $array['value'] : null);
	$name = (isset($array['name']) ? $array['name'] : null);
	$id = (isset($array['id']) ? $array['id'] : null);
	$length = (isset($array['length']) ? $array['length'] : 20);
	$checked = (!empty($array['checked']) ? 'checked' : null);
	$sample = (isset($array['sample']) ? '<span class="formsample">'.$array['sample'].'</span>' : null);
	switch($array['type']) 
	{
		case "text":
		$html = "<input type=\"text\" size=\"$length\" maxlength=\"$length\" name=\"$name\" value=\"$val\" /> $sample";
		break;
		case "checkbox":
		$html = "<input type=\"checkbox\" ".$checked." name=\"$name\" value=\"$val\" />";
		break;
		case "radio":
		$html = "";
		break;
		case "submit":
		$html = "<input type=\"submit\" name=\"$name\" value=\"$val\" />";
		break;
		case "hidden":
		$html = "<input type=\"hidden\" name=\"$name\" value=\"$val\" />";
		break;
		case "password":
		$html = "<input type=\"password\" size=\"$length\" maxlength=\"$length\" name=\"$name\" value=\"$val\" /> $sample";
		break;
	}
	return $html;
}

function form_generator($array)
{
	foreach($array as $form)
	{
		if(!isset($form['0']))
		{
			$form_array[] = form_input($form);
		}
		else
		{
			$mult = "";
			foreach($form as $value) $mult .= form_input($value);
			$form_array[] = $mult;
		}
	}
	return($form_array);
}



require('/usr/local/php5/smarty/Smarty.class.php');
require('/Users/denk/Sites/sacrs/functions/function_list.php');
$smarty = new Smarty();

if(isset($_POST['formsent'])) 
{ 
	$post_user = $_POST['user'];
	$salt = rand_ascii(2);
	$_POST['user']['password'] = crypt($post_user['password'], $salt);
	$post_user = $_POST['user'];
	$salt = rand_ascii(2);
	$key = "";
	$value = "";
	foreach($_POST['user'] as $userkey => $uservalue)
	{
		$key .= "'$userkey', ";
		$value .= "'$uservalue', ";
	}
	$insert = "INSERT INTO user ($key 'created', 'last_login', 'modified') VALUES ($value 'NOW()', 'NOW()', 'NOW()')";
	$status = (db_insert($insert)) ? $status[] = "Account Created!" : $status[] = "There was an error creating the account. Please do it right next time.";
	
}

//Form Field Descriptions:
if(isset($_GET['reg']) && $_GET['reg']=="true" && !isset($_POST['formsent'])) {
	$smarty->assign('names',array('First Name','Last Name','Username','Email Address','Password','Confirm Password','Nickname','Address','','City','State','Zip','Phone','Over 13?'));
	$form = array(	array('type'=>"text", 'name'=>"user[f_name]"),
					array('type'=>"text", 'name'=>"user[l_name]", 'length'=>"20"),
					array('type'=>"text", 'name'=>"user[user_name]", 'length'=>"20"),
					array('type'=>"text",'name'=>"user[email]",'sample'=>"abc1@sacrs.com"),
					array('type'=>"password",'name'=>"user[password]"),
					array('type'=>"password",'name'=>"user[password2]"),
					array('type'=>"text", 'name'=>"user[nickname]", 'length'=>"20"),
					array('type'=>"text",'name'=>"user[address_1]", 'length'=>"30"),
					array('type'=>"text",'name'=>"user[address_2]", 'length'=>"30"),
					array('type'=>"text", 'name'=>"user[city]", 'length'=>"20"),
					array('type'=>"text", 'name'=>"user[state]", 'length'=>"2"),
					array('type'=>"text", 'name'=>"user[zip]", 'length'=>"5"),
					array(	array('type'=>"text", 'name'=>"user[country]", 'length'=>"1"),
							array('type'=>"text", 'name'=>"user[area_code]", 'length'=>"3"),
							array('type'=>"text", 'name'=>"user[phone3]", 'length'=>"3"),
							array('type'=>"text", 'name'=>"user[phone4]", 'length'=>"4")),
					array('type'=>"checkbox", 'name'=>"user[over13]", 'value'=>"true"),
					array('type'=>"hidden", 'value'=>"formsent", 'name'=>"formsent"),
					array('type'=>"submit",'value'=>"Create Account"));
}
else {
	$smarty->assign('names',array('Username','Password'));
	$form = array(	array('type'=>"text",'name'=>"username"),
					array('type'=>"password", 'name'=>"password"),
					array('type'=>"submit",'value'=>"Log In"));
}
//Form Field Display Processing
$form_array = form_generator($form);
//Form Field HTML
$reg = (isset($_GET['reg']) ? $_GET['reg'] : 'false');

if(!empty($status)) $smarty->assign('status', $status);
$smarty->assign('reg', $reg);
$smarty->assign('me', $_SERVER['REQUEST_URI']);
$smarty->assign('fields', $form_array);
$smarty->display('login.tpl');

if(isset($insert)) echo $insert; 

?>